#! /usr/bin/python
#######################################################################################
#                                                                                     #
#    File: ad_auto.py                                                                 #
#    Part of 2dgal-cheater                                                            #
#    Home: http://2dgal-cheater.googlecode.com                                        #
#                                                                                     #
#    The MIT License                                                                  #
#                                                                                     #
#    Copyright (c) 2010-2011 <araya.akashic@gmail.com>                                #
#                                                                                     #
#    Permission is hereby granted, free of charge, to any person obtaining a copy     #
#    of this software and associated documentation files (the "Software"), to deal    #
#    in the Software without restriction, including without limitation the rights     #
#    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell        #
#    copies of the Software, and to permit persons to whom the Software is            #
#    furnished to do so, subject to the following conditions:                         #
#                                                                                     #
#    The above copyright notice and this permission notice shall be included in       #
#    all copies or substantial portions of the Software.                              #
#                                                                                     #
#    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR       #
#    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,         #
#    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE      #
#    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER           #
#    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,    #
#    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN        #
#    THE SOFTWARE.                                                                    #
#                                                                                     #
#######################################################################################
# This code is to automatically click ads on kf bbs

# Author: Araya

import time

import urllib
import urllib2

from browser_bogus import Browser


#customized these
#adding account as "username":"password"
account_list = {"":"",
                "":""}

#total attempts
cycle = 1
#wait 1 hr 10 s before each attempt
delay = 3600 * 5

#do not change below this
siteurl = "http://bbs.2dgal.com"
login_page = "login.php"
star1_page = "kf_star_1.php"

#ad link character
ad_char = "diy_ad_move.php"


#method login
def login(user):
    logindata = {"pwuser":user.username,
                 "pwpwd":user.password,
                 "hideid":"0",
                 "cktime":"0",
                 "forward":"index.php",
                 "jumpurl":"index.php",
                 "step":"2"}
    
    en_logindata = urllib.urlencode(logindata)
    
    url = siteurl + "/" + login_page
    request = urllib2.Request(url)
    result = user.urlOpener.open(request)
    #print "Getting login page"
    url = url + "?"
    request = urllib2.Request(url, en_logindata)
    result = user.urlOpener.open(request)
    print "Login " + user.username
    #print cookie
    
def ad_auto(user):
    question = []
    answer = ""
    #print "Getting star 1 question"
    url = siteurl + "/" + star1_page
    request = urllib2.Request(url)
    result = user.urlOpener.open(request)
    
    pagelines = result.readlines()
    #print len(pagelines)
    #extracting logout URI
    line = pagelines[74]
    user.logout_URI = line.split("=\"")[-1].split("\"")[0]
   
    for line in pagelines:
        match = line.find(ad_char)
        if match >= 0:
            words = line.split("=\"")
            for word in words:
                if word.find(ad_char) >= 0:
                    user.ad_link = word.split("\"")[0]
                    print user.ad_link
    
    url = siteurl + "/" + user.ad_link
    request = urllib2.Request(url)
    result = user.urlOpener.open(request)
    print "Ad click sent"
    result_data = result.read()
    return result_data.find("<meta http-equiv=\"refresh\"")
#    return 1

    
    
def logout(user):
    url = siteurl + "/" + user.logout_URI
    request = urllib2.Request(url)
    result = user.urlOpener.open(request)
    print "Logout " + user.username
    
def perform_ad_auto():
    for n, p in account_list.iteritems():
        user = Browser(n, p)
        submit_success = -1
        try:
            login(user)
            while submit_success < 0:
                submit_success = ad_auto(user)
                print submit_success
            logout(user)
        except:
            pass
    
if __name__ == "__main__":
    while cycle:
        print "------------------------------------------"
        print time.strftime("%a, %b %d %Y %H:%M:%S", time.localtime()) + "-----------------"
        perform_ad_auto()
        if cycle > 1:
            time.sleep(delay)
        cycle = cycle - 1